// pages/survey/survey.js
const app = getApp()
let reqJs = require('../../utils/req.js')
var flag = true //提交按钮限制


Page({

    /**
     * 页面的初始数据
     */
    data: {
        fromData: {

        },
        reqData: [{
                "id": 13,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 1,
                "list": [{
                    "id": 26,
                    "title": "年龄：",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 1,
                    "min_requred": 1,
                    "max_requred": 1,
                    "items": [{
                            "id": 129,
                            "content": "18-25",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 130,
                            "content": "26-30",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 131,
                            "content": "31-40",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 132,
                            "content": "41-50",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                      
                        {
                            "id": 134,
                            "content": "50-61",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 135,
                            "content": "61以上",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        }
                    ]
                }]
            },
            {
                "id": 23,
                "name": "关于广汽丰田展台，带给您深刻印象的是：",
                "type": 3,
                "check_type": 1,
                "is_required": 1,
                "list": [
                    {
                        "id": 44,
                        "title": "",
                        "type": 0,
                        "is_check": 0,
                        "rationale": "",
                        "is_requred": 0,
                        "min_requred": 1,
                        "max_requred": 0,
                        "items": [
                            {
                                "id": 226,
                                "content": "互动奖品",
                                "is_check": 0,
                                "is_write": 0,
                                "sort": 0
                            },
                        
                            {
                                "id": 228,
                                "content": "服务态度",
                                "is_check": 0,
                                "is_write": 0,
                                "sort": 0
                            },
                            {
                                "id": 229,
                                "content": "座椅舒适",
                                "is_check": 0,
                                "is_write": 0,
                                "sort": 0
                            },
                            {
                                "id": 230,
                                "content": "其他",
                                "is_check": 0,
                                "is_write": 1,
                                "sort": 0
                            }
                        ]
                    },
                   
                ]
            },
            {
                "id": 16,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 1,
                "list": [{
                    "id": 34,
                    "title": "请问在展台的全部车辆中，您更有意向购买哪一款",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 1,
                    "min_requred": 1,
                    "max_requred": 1,
                    "items": [{
                            "id": 168,
                            "content": "赛那",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 169,
                            "content": "汉兰达",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 170,
                            "content": "威兰达",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 171,
                            "content": "威飒",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 172,
                            "content": "锋兰达",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 173,
                            "content": "凯美瑞",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 174,
                            "content": "雷凌",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 175,
                            "content": "铂智4X",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 176,
                            "content": "均无购买意向",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        }
                    ]
                }]
            },
         
            {
                "id": 19,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 0,
                "list": [{
                    "id": 38,
                    "title": "请问您认为全新第9代凯美瑞最吸引您的是哪点？",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 0,
                    "min_requred": 0,
                    "max_requred": 0,
                    "items": [{
                            "id": 192,
                            "content": "车身颜色",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 193,
                            "content": "车身线条感",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 194,
                            "content": "乘坐空间",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 195,
                            "content": "乘坐舒适性",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 196,
                            "content": "智能座舱（车机灵敏、精准，应用丰富、便利等）",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 197,
                            "content": "豪华配置（音乐联动氛围灯、智能香薰系统）",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 198,
                            "content": "智能驾驶辅助系统",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 199,
                            "content": "其他",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        }
                    ]
                }]
            },
            {
                "id": 20,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 0,
                "list": [{
                    "id": 39,
                    "title": "请问您认为全新第9代凯美瑞最需改善的是哪点？",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 0,
                    "min_requred": 0,
                    "max_requred": 0,
                    "items": [{
                            "id": 200,
                            "content": "车身颜色",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 201,
                            "content": "车身线条感",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 202,
                            "content": "乘坐空间",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 203,
                            "content": "乘坐舒适性",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 204,
                            "content": "智能座舱（车机灵敏、精准，应用丰富、便利等）",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 205,
                            "content": "豪华配置（音乐联动氛围灯、智能香薰系统）",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 206,
                            "content": "智能驾驶辅助系统",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 207,
                            "content": "其他",
                            "is_check": 0,
                            "is_write": 1,
                            "sort": 0
                        }
                    ]
                }]
            },
            {
                "id": 21,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 1,
                "list": [{
                    "id": 40,
                    "title": "本次赛那改装版与您心目中期待的改装效果符合吗？",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 1,
                    "min_requred": 1,
                    "max_requred": 1,
                    "items": [{
                            "id": 208,
                            "content": " 完全符合",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 209,
                            "content": "比较符合",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 210,
                            "content": "一般符合",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 211,
                            "content": "不是很符合",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 212,
                            "content": "完全不符合",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        }
                    ]
                }]
            },
            {
                "id": 22,
                "name": "",
                "type": 1,
                "check_type": 0,
                "is_required": 1,
                "list": [{
                    "id": 41,
                    "title": "本次赛那的改装内容（不含航空座椅、后排吸顶屏），您愿意花费多少钱购买？",
                    "type": 0,
                    "is_check": 0,
                    "rationale": "",
                    "is_requred": 1,
                    "min_requred": 1,
                    "max_requred": 1,
                    "items": [{
                            "id": 213,
                            "content": "1.5-2万元",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 214,
                            "content": "2-2.5万元",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 215,
                            "content": "2.5-3万元",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        },
                        {
                            "id": 216,
                            "content": "3万元以上",
                            "is_check": 0,
                            "is_write": 0,
                            "sort": 0
                        }
                    ]
                }]
            },
          
    
        ],


        type3radioChangeIndex: -1, //默认不选中 
        type3checkboxChangeChecked: [], //选中值
        type3radioChangeIndex2: -1, //默认不选中 
        type3checkboxChangeChecked2: [], //选中值


        questionnairePushData: {}, //提交后返回的数据

        // 盖章弹窗开关
        ElecSealSmallPopupShow: false, //测试显示弹窗
        ElecSealSmallPopupData: {
            is_use: 0, //是否已盖章
            title: "恭喜获得问卷礼1份",
            verification_code: "", //核销码
        }, //传入的弹窗组件的数据


        input7_7: "",
        input8_7: "",
        // input11_3:"" //



        // 留资信息数据
        nameVal: '', //姓名
        phoneVal: '', //手机号
        codeVal: '', //验证码
        isCheck: false, //是否选择隐私协议
        isSend: false, //是否发送验证码
        sendTime: '', //倒计时时间
        isSuo: true,

        qtspShow: true,
        getTaskList:[{},{}],
        // qtspShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // this.questionnaire()
    },
    closeMode() {
        this.setData({

            qtspShow: false,
        })
    },
    tapChe(e){
        console.log(e)
        let selectIndex = e.currentTarget.dataset.index
        this.setData({
            selectIndex,
        })
    },
    // 获取验证码
    getCodeNum() {
        let that = this
        console.log('>>>>>>>>>>>获取到验证码')
        let preg = /^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9]|19[0-9]|16[0-9])[0-9]{8}$/
        if (that.data.phoneVal == '' || !preg.test(that.data.phoneVal)) {
            wx.showToast({
                title: '手机号格式错误!',
                icon: 'none'
            })
            return
        }
        let data = {
            phone: that.data.phoneVal
        }

        app.$http.post('/user/phone/send', data, false, "").then(res => {
            if (res.code === 200) {
                wx.showToast({
                    title: '验证码已发送.请注意接收',
                    icon: 'none',
                })
                let time = 60;
                var timers = setInterval(function () {
                    time--;
                    if (time > 0) {
                        that.setData({
                            sendTime: time,
                            isSend: true
                        });
                    } else {
                        that.setData({
                            isSend: false,
                            sendTime: '',
                        });
                        clearInterval(timers)
                    }
                }, 1000)
            } else {
                wx.showToast({
                    title: res.message,
                    icon: "none"
                })
            }
        })
    },



    closeModeAi() {
        wx.reLaunch({
            url: '/pages/index/index',
        })
    },
    questionnaire() {
        // 
        reqJs.questionnaire().then(res => {

            console.log('>>>>>', res)

            let reqData = res.data
            this.setData({
                reqData,
            })

        }).catch(error => {
            console.log(error)
        })
    },
    // type2的输入框
    bindblurType2(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        let reqdataindex = e.currentTarget.dataset.reqdataindex
        const rationale = e.detail.value;

        let reqData = this.data.reqData


        reqData[reqdataindex].list[index].rationale = rationale


        this.setData({
            reqData,
        })
        console.log(reqData)
        console.log(this.data)
    },
    // 点击触发设置其他是选中
    bindblur(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        let reqdataindex = e.currentTarget.dataset.reqdataindex

        let reqData = this.data.reqData


        reqData[reqdataindex].list[0].items[index].is_check = 1


        this.setData({
            reqData,
        })
        console.log(reqData)
        console.log(this.data)
    },
    onConfirm(e) {
        console.log(e)

        if (!flag) {
            return
        }

        let a_id1 = e.detail.value.a_id1
        let a_id2 = e.detail.value.a_id2
        let a_id4 = e.detail.value.a_id4


        // 单选框中含有其他 输入框（非必填）
        let a_id7 = e.detail.value.a_id7
        let a_id8 = e.detail.value.a_id8
        // 单选框中含有其他 输入框（非必填）

        let a_id9 = e.detail.value.a_id9
        let a_id10 = e.detail.value.a_id10
        let a_id12 = e.detail.value.a_id12


        let input3_0 = e.detail.value.input3_0
        let input3_1 = e.detail.value.input3_1
        let input3_2 = e.detail.value.input3_2
        let input3_3 = e.detail.value.input3_3
        let input3_4 = e.detail.value.input3_4
        let input3_5 = e.detail.value.input3_5

        let input5_0 = e.detail.value.input5_0

        let input6_0 = e.detail.value.input6_0
        let input6_1 = e.detail.value.input6_1

        let input7_7 = e.detail.value.input7_7
        let input8_7 = e.detail.value.input8_7
        let input11_3 = e.detail.value.input11_3






        let type3checkboxChange11_0 = e.detail.value.type3checkboxChange11_0
        let type3checkboxChange11_1 = e.detail.value.type3checkboxChange11_1
        let type3checkboxChange11_2 = e.detail.value.type3checkboxChange11_2





        // 这里先做校验是否有选中

        // if (a_id1 == '' || a_id2 == '' || a_id4 == ''  || a_id7 == '' || a_id8 == ''|| a_id9 == '' || a_id10 == '' || a_id12 == '') {
        if (a_id1 == '' || a_id2 == '' || a_id4 == '' || a_id9 == '' || a_id10 == '' || a_id12 == '') {
            wx.showToast({
                title: '请选择答案~',
                icon: "none"
            })
            return
        }


        if (this.data.star3_1 == 0 || this.data.star3_2 == 0 || this.data.star3_3 == 0 || this.data.star3_4 == 0 ||
            this.data.star3_5 == 0 || this.data.star3_6 == 0 || this.data.star5_1 == 0 || this.data.star6_1 == 0 ||
            this.data.star6_2 == 0) {
            wx.showToast({
                title: '请选择评分~',
                icon: "none"
            })
            return
        }
        if (type3checkboxChange11_0.length == 0 && type3checkboxChange11_1.length == 0 && type3checkboxChange11_2.length == 0 && input11_3 == "") {
            wx.showToast({
                title: '请选择答案噢~',
                icon: "none"
            })
            return
        }
        // if (this.data.type3radioChangeIndex == -1 || this.data.type3radioChangeIndex2 == -1) {
        //     wx.showToast({
        //         title: '请选择答案噢~',
        //         icon: "none"
        //     })
        //     return
        // }

        // if (type3checkboxChange.length == 0 || type3checkboxChange2.length == 0) {
        //     wx.showToast({
        //         title: '请选择多选答案噢~',
        //         icon: "none"
        //     })
        //     return
        // }


        let reqData = this.data.reqData

        // if (this.data.type3radioChangeIndex != -1 || this.data.type3radioChangeIndex != -1) {
        //     reqData.forEach(item => {

        //         if (item.id == 7) {

        //             // 表单数据
        //             item.list[this.data.type3radioChangeIndex].items.forEach(itemA => {

        //                 // 选中数组
        //                 type3checkboxChange.forEach(itemB => {
        //                     if (itemA.id == itemB) {
        //                         itemA.is_check = 1
        //                     }
        //                 });



        //             })



        //         }
        //         if (item.id == 8) {

        //             // 表单数据
        //             item.list[this.data.type3radioChangeIndex2].items.forEach(itemA => {

        //                 // 选中数组
        //                 type3checkboxChange2.forEach(itemB => {
        //                     if (itemA.id == itemB) {
        //                         itemA.is_check = 1
        //                     }
        //                 });



        //             })



        //         }




        //     });
        // }



        console.log(reqData)

        let fromData = []

        reqData.forEach(item => {
            // console.log( item.list)
            item.list.forEach(itemB => {
                fromData.push(itemB)
            });

        });

        console.log(fromData)

        fromData.forEach(item => {

            if (item.id == 38) {
                item.items[7].content = input7_7
            }
            if (item.id == 39) {
                item.items[7].content = input8_7
            }


            if (item.id == 42) {

                item.items.forEach(itemB => {

                    type3checkboxChange11_0.forEach(itemC => {

                        if (itemB.id == itemC) {
                            itemB.is_check = 1
                        }

                    })



                })

            }

            if (item.id == 43) {

                item.items.forEach(itemB => {

                    type3checkboxChange11_1.forEach(itemC => {

                        if (itemB.id == itemC) {
                            itemB.is_check = 1
                        }

                    })



                })

            }
            if (item.id == 44) {

                item.items.forEach(itemB => {

                    type3checkboxChange11_2.forEach(itemC => {

                        if (itemB.id == itemC) {
                            itemB.is_check = 1
                        }

                    })



                })

            }
            if (item.id == 45) {
                item.items[0].content = input11_3
                item.items[0].is_check = 1
            }


        });




        // this.setData({
        //     reqData,
        // })
        console.log(fromData)

        reqJs.questionnairePush(fromData).then(res => {

            console.log('>>>>>', res)

            let questionnairePushData = res.data
            this.setData({
                questionnairePushData,
            })

            let ElecSealSmallPopupData = this.data.ElecSealSmallPopupData
            ElecSealSmallPopupData.is_use = 0
            ElecSealSmallPopupData.verification_code = questionnairePushData.code

            this.setData({
                ElecSealSmallPopupData,
                ElecSealSmallPopupShow: true
            })


        }).catch(error => {
            console.log(error)
        })



    },

    type3checkboxChange(e) {
        console.log(e)

        let index = e.currentTarget.dataset.index
        let reqdataindex = e.currentTarget.dataset.reqdataindex
        const selectedOptions = e.detail.value;

        let str = `type3checkboxChangeChecked` + (reqdataindex + 1) + '_' + (index + 1)


        this.setData({
            [str]: selectedOptions
        })



        // if (selectedOptions.length <= 3) {
        //     this.setData({
        //       type3checkboxChangeChecked: selectedOptions
        //     });
        //   } else {
        //     // 如果选择的选项超过3个，将选中的选项限制在前3个
        //     this.setData({
        //         type3checkboxChangeChecked: selectedOptions.slice(0, 3)
        //     });
        //   }

        console.log(this.data)

    },


    radioChange(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        let value = e.detail.value

        let reqData = this.data.reqData
        console.log(index)
        console.log(value)

        reqData[index].list[0].items.forEach((i, index) => {

            if (value == i.id) {
                i.is_check = 1
            } else {
                i.is_check = 0
            }

        });


        if (index == 6 && value != reqData[index].list[0].items[7].id) {

            this.setData({
                input7_7: ""
            })

        }
        if (index == 7 && value != reqData[index].list[0].items[7].id) {

            this.setData({
                input8_7: ""
            })

        }



        this.setData({
            reqData,
        })
        console.log(reqData)
        console.log(this.data)


    },
    PingFenTap(e) {
        console.log(e)
        let reqdataindex = e.currentTarget.dataset.reqdataindex
        let index = e.currentTarget.dataset.index
        let star = e.currentTarget.dataset.star + 1


        let reqData = this.data.reqData



        for (let i = 0; i < 5; i++) {

            reqData[reqdataindex].list[index].items[i].is_check = 0

        }
        for (let i = 0; i < star; i++) {

            reqData[reqdataindex].list[index].items[i].is_check = 1

        }
        console.log(reqData[reqdataindex].list[index].items)


        console.log(star);

        let str = `star` + (reqdataindex + 1) + '_' + (index + 1)

        this.setData({
            [str]: star
        })

        // if (index == 1) {
        //     let str = `star`+ index
        //     this.setData({
        //         star1: star
        //     })
        // }
        // if (index == 2) {
        //     this.setData({
        //         star2: star
        //     })
        // }
        // if (index == 3) {
        //     this.setData({
        //         star3: star
        //     })
        // }


        // if (index == 4) {
        //     this.setData({
        //         star4: star
        //     })
        // }
        // if (index == 5) {
        //     this.setData({
        //         star5: star
        //     })
        // }
        // if (index == 6) {
        //     this.setData({
        //         star6: star
        //     })
        // }


        this.setData({
            reqData,
        })

        console.log(this.data)
    },

    type3radioChange(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let reqdataindex = e.currentTarget.dataset.reqdataindex
        let value = e.detail.value
        let type3radioChangeIndex
        // console.log(index)
        console.log(value)

        item.forEach((item, index) => {
            if (value == item.id) {
                type3radioChangeIndex = index
            }
        });

        if (type3radioChangeIndex == this.data.type3radioChangeIndex) {
            return
        }


        let reqData = this.data.reqData

        reqData[reqdataindex].list.forEach((item, index) => {



            //选中单选框的时候 需要把他底下的多选框数组选中状态清空
            item.items.forEach(itemB => {

                itemB.is_check = 0

            });


            if (value == item.id) {
                item.is_check = 1
            } else {
                item.is_check = 0
            }


        })


        this.setData({
            type3radioChangeIndex,
            reqData,
            type3checkboxChangeChecked: false
        })

        console.log(this.data.reqData)
    },
    type3radioChange2(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let reqdataindex = e.currentTarget.dataset.reqdataindex
        let value = e.detail.value
        let type3radioChangeIndex2
        // console.log(index)
        console.log(value)

        item.forEach((item, index) => {
            if (value == item.id) {
                type3radioChangeIndex2 = index
            }
        });

        if (type3radioChangeIndex2 == this.data.type3radioChangeIndex2) {
            return
        }

        let reqData = this.data.reqData

        reqData[reqdataindex].list.forEach((item, index) => {

            if (value == item.id) {
                item.is_check = 1
            } else {
                item.is_check = 0
            }


        })
        this.setData({
            type3radioChangeIndex2,
            reqData,
            type3checkboxChangeChecked2: false
        })
        console.log(this.data.reqData)
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})